package com.zp.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zp.mall.product.entity.AttrGroupEntity;
import com.zp.mall.product.service.AttrGroupService;
import com.zp.mall.product.service.CategoryService;
import com.zp.mall.product.vo.AttrEditResponseVo;
import com.zp.mall.product.vo.AttrGroupRelationVo;
import com.zp.mall.product.vo.AttrRequestVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zp.mall.product.entity.AttrEntity;
import com.zp.mall.product.service.AttrService;
import com.zp.common.utils.PageUtils;
import com.zp.common.utils.R;
import org.w3c.dom.Attr;


/**
 * 商品属性
 *
 * @author zhangpeng
 * @email zhangpeng@aliyun.com
 * @date 2020-10-11 17:08:57
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrGroupService attrGroupService;
    /**
     * 获取分类规格参数
     * /product/attr/base/list/{catelogId}  查询基本属性
     * /product/attr/sale/list/{catelogId}  查询销售属性
     */
    @RequestMapping("/{attrTypePath}/list/{catelogId}")
    //@RequiresPermissions("product:attr:list")
    public R attrBaseList(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId, @PathVariable("attrTypePath") String attrTypePath){
        PageUtils page = attrService.queryBaseAttrPage(params, catelogId, attrTypePath);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrEditResponseVo attr = attrService.getDetailAttrInfo(attrId);
        attr.setCatelogPath (categoryService.findCatelogPath (attr.getCatelogId ()));  //获取分类类别路径 例：[2,25,255]
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrRequestVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrRequestVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeAttrByIds(attrIds);

        return R.ok();
    }

}
